package com.oocl.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.factory.ServiceFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Client;
import com.oocl.service.ClientService;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ClientService clientService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		clientService=ServiceFactory.getClientService();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		String confirmpassword=request.getParameter("confirmpassword");
		Log4j.getLogger().info("user {} login in", userName);
		Log4j.getLogger().info("password {} login in", password);
		Log4j.getLogger().info("confirmpassword {} login in", confirmpassword);
		boolean isError=false;
		if (userName==null||"".equals(userName.trim())) {
			request.setAttribute("error", 1);
			isError=true;
		}
		else if(password==null||"".equals(password.trim())){
			request.setAttribute("error", 2);
			isError=true;
		}
		else if(confirmpassword==null||"".equals(confirmpassword.trim())){
			request.setAttribute("error", 3);
			isError=true;
		}
		else if (!password.equals(confirmpassword)) {
			request.setAttribute("error", 4);
			isError=true;
		}
		else{
			//先查询是否重名
			Client client=clientService.queryByName(userName);
			if (client==null) {
				//如果不重名
				Client c=new Client(userName, password);
				int m=clientService.saveClient(c);
				if (m>0) {
					Log4j.getLogger().info("[ client ] {} 注册成功。", c.toString());
					response.sendRedirect("login.jsp");
				}
				else{
					request.setAttribute("error", 6);
					isError=true;
				}
			}else{
				request.setAttribute("error", 5);
				isError=true;
			}
		}
		if (isError) {
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
		
		
	}

}
